package com.itheima.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.R;
import com.itheima.api.UmsMenuApi;
import com.itheima.api.UmsRoleMenuRelationApi;
import com.itheima.pojo.UmsMenu;
import com.itheima.vo.PageResult;
import com.itheima.vo.UmsMenuNode;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * @author 袁鹏
 * @date 2022-10-13-12:43
 */
@Service
public class UmsMenuService {

    @DubboReference
    private UmsMenuApi umsMenuApi;

    @DubboReference
    private UmsRoleMenuRelationApi umsRoleMenuRelationApi;


    /**
     * 添加后台菜单
     */
    public void create(UmsMenu umsMenu) {
        umsMenuApi.create(umsMenu);
    }


    /**
     * 根据ID删除后台菜单
     */
    public void delete(Long id) {
        // 获取当前目录所有子级目录id
        List<Long> idList = umsMenuApi.getChildId(id);
        // 添加当前目录id进待删除目录id列表
        idList.add(id);
        // 远程调用删除目录
        umsMenuApi.delete(idList);
        // 远程调用删除目录和角色的关联
        umsRoleMenuRelationApi.deleteByMenuId(idList);
    }


    /**
     * 分页查询后台菜单
     */
    public PageResult pageByParentId(Long parentId, Integer pageNum, Integer pageSize) {
        Page<UmsMenu> pageInfo = umsMenuApi.page(parentId, pageNum, pageSize);
        return new PageResult<>(Convert.toInt(pageInfo.getCurrent()), Convert.toInt(pageInfo.getSize()), Convert.toInt(pageInfo.getTotal()), Convert.toInt(pageInfo.getCurrent()), pageInfo.getRecords());
    }


    /**
     * 树形结构返回所有菜单列表
     */
    public List<UmsMenuNode> treeList() {
        // 远程调用查询所有菜单
        List<UmsMenuNode> menuList = umsMenuApi.list().stream().map(UmsMenuNode::init).collect(Collectors.toList());
        // 创建父id映射集合
        Map<Long, List<UmsMenuNode>> parentIdMap = new HashMap<>();
        // 遍历菜单集合, 添加到映射中
        for (UmsMenuNode menuNode : menuList) {
            if(parentIdMap.containsKey(menuNode.getParentId())){
                parentIdMap.get(menuNode.getParentId()).add(menuNode);
            }else{
                List<UmsMenuNode> temp = new ArrayList<>();
                temp.add(menuNode);
                parentIdMap.put(menuNode.getParentId(), temp);
            }
        }
        // 获取最高集目录列表
        List<UmsMenuNode> headMenuList = parentIdMap.get(0L);
        if(CollUtil.isNotEmpty(headMenuList)){
            // 调用递归方法, 生成树型结构
            headMenuList.forEach(h -> h.setChildren(getChilden(h.getId(), parentIdMap)));
        }
        return headMenuList;
    }

    private List<UmsMenuNode> getChilden(Long pid, Map<Long, List<UmsMenuNode>> parentIdMap){
        List<UmsMenuNode> children = parentIdMap.get(pid);
        if(CollUtil.isNotEmpty(children)){
            children.forEach(c -> c.setChildren(getChilden(c.getId(), parentIdMap)));
        }
        return children;
    }

    public R findById(Integer id) {
        UmsMenu umsMenu=umsMenuApi.findById(id);
        return R.success(umsMenu);
    }

    public R updateById(Integer id, UmsMenu umsMenu) {
        umsMenuApi.updateById(id,umsMenu);
        return R.success(null);
    }

    public R updateHidden(Integer id, Integer hidden) {
        umsMenuApi.updateHidden(id,hidden);
        return R.success(null);
    }
}
